package iscript.gameFramework.GameLite.graphics.animation
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import iscript.coreFramework.interfaces.IDisposable;
	import iscript.gameFramework.GameLite.events.BitmapBatchEvent;
	import iscript.gameFramework.GameLite.Game;
	
	
	[Event(name = "dispose", type = "iscript.gameFramework.GameLite.events.BitmapBatchEvent")]
	
	public class BitmapBatch extends Bitmap implements IAnimation
	{
		
		public function BitmapBatch(data:Vector.<BitmapData>)
		{
			_bitmapdataSource = data;
			_loop = true;
			_disposed = false;
			fps = Game.graphicsDeviceManager.fps;
			this.bitmapData == _bitmapdataSource[0];			
			Game.graphicsDeviceManager.bitmapBatchManager.add(this);
		}
		
		private var _disposed:Boolean;
		private var _pause:Boolean;
		private var _isStrat:Boolean;
		private var _bitmapBatchTaskAction:BitmapBatchTaskAction;
		private var _currentFrame:int = 0;
		private var _bitmapdataSource:Vector.<BitmapData>;
		private var _fps:int;
		private var _loop:Boolean;
		
		public function get IsinStage():Boolean{return  this.stage!=null}
		
 
		
		public function get TotalFrame():int
		{
			return _bitmapdataSource.length;
		}
		
		public function get currentFrame():int
		{
			return _currentFrame;
		}
		
		public function set currentFrame(value:int):void
		{
		    if (value < 0){_currentFrame = 0;}
			if (value >= TotalFrame) {_currentFrame = TotalFrame - 1;}
		 	this.bitmapData = _bitmapdataSource[_currentFrame];
		}
		
		public function get IsStart():Boolean
		{
			return _isStrat;
		}
		
		public function get loop():Boolean { return _loop; }
		public function set loop(value:Boolean):void { _loop = value; }
		
		public function frameRenderLogic():void
		{
			if (!_pause && !_disposed )
			{				
				// IF 效率高于 Switch 大量判断switch会降帧
				if (_loop) {
						this.bitmapData = _bitmapdataSource[_currentFrame];
						_currentFrame++;
						_currentFrame = _currentFrame == TotalFrame?0:_currentFrame;	
					}else {

					if (_currentFrame != TotalFrame-1) {
							this.bitmapData = _bitmapdataSource[_currentFrame];
						     _currentFrame++;
							}
				}
				
			    //INFO:如果不包含在Stage中,自动隐藏。
				/*var rect:Rectangle = new Rectangle(0, 0,
				                                   Game.graphicsDeviceManager.stage.stageWidth,
				                                   Game.graphicsDeviceManager.stage.stageHeight);
				var point:Point =  Game.graphicsDeviceManager.toDevicePoint(this);
				var thisViewRect:Rectangle = new Rectangle(point.x, point.y, this.width, this.height);
				
				this.visible = rect.intersects(thisViewRect);*/
				
			}
		}
		
		public function play():void
		{
			this.bitmapData = _bitmapdataSource[_currentFrame];
			_isStrat = true;
			_disposed = false;
			_pause = false;
		}
		
		public function pause():void
		{
			_pause = !_pause;
		}
		
		public function stop():void
		{
		  _pause = true;
		}
		
		
		
		public function get Disposed():Boolean
		{
			return _disposed;
		}
		
		public function get fps():int 
		{
			return _fps;
		}
		
		public function set fps(value:int):void 
		{
			_fps = value;
			if(TotalFrame>1){
				if(_bitmapBatchTaskAction!=null){
					 Game.TimeUpdate.removeTaskAction(_bitmapBatchTaskAction);
				}
				_bitmapBatchTaskAction = new BitmapBatchTaskAction(this);
				Game.TimeUpdate.addTaskAction(_bitmapBatchTaskAction, 1000 / _fps);
			}
		}
		
		public function get BitmapDataSource():Vector.<BitmapData> 
		{
			return _bitmapdataSource;
		}
		
		public function set BitmapDataSource(value:Vector.<BitmapData>):void 
		{
			_bitmapdataSource = value;
			currentFrame = 0;
		}
		
		public function clone():BitmapBatch {
			 return  new BitmapBatch(_bitmapdataSource);
			}
		
		public function dispose():void
		{
			if (!_disposed)
			{
				_disposed = true;
				var event:BitmapBatchEvent = new BitmapBatchEvent(BitmapBatchEvent.DISPOSE);
				    dispatchEvent(event);
				this.bitmapData = null
				if (_bitmapBatchTaskAction != null)
				Game.TimeUpdate.removeTaskAction(_bitmapBatchTaskAction);
			}
		}
	
	}
}



import iscript.gameFramework.GameLite.graphics.animation.BitmapBatch;
import iscript.gameFramework.GameLite.thread.task.TaskAction;

internal class BitmapBatchTaskAction extends TaskAction
{
	
	private var _bitmapBatch:BitmapBatch
	
	public function BitmapBatchTaskAction(bitmapBatch:BitmapBatch)
	{
		_bitmapBatch = bitmapBatch;
	}
	
	override public function execute():void
	{
		super.execute();
		if (_bitmapBatch != null && _bitmapBatch.IsStart)
		{
			_bitmapBatch.frameRenderLogic();
		}
	}

}
